% GF_VECTOR(M, PRIM_POLY)  Construct the field GF(2^m) in vector form 
% using the primitive polynomial stored in vector form in prim_poly
%
% Input: 
%
%   prim_poly  Primitive m'th polynomial in GF(2^m).  Use a root of this
%              polynomial to construct the vector space.
%
% Output:
%   v          Vector space representation of GF(2^m) (in order of
%              increasing powers of x)

function v = gf_vector(prim_poly)

    global m

    % The primitive polynomial in prim_poly has some root a.  If prim_poly
    % is m'th order, this gives the equation 
    %
    %    a^m = g(a)
    %
    % where g is a binary polynomial such that ord(g) < m.  Therefore, we
    % can start with a^0 = 1000...(binary) and shift the 1 to the left
    % until it is in the (m+1)'th position (a^m).  At that time, remove the
    % 1 and add g(a).  Repeat until 2^m - 1 is reached.

    g = [prim_poly(1:end-1) 0];  % Get the polynomial g
    poly = zeros(size(g));       % Initialize the working polynomial
    poly(1) = 1;                 % Init the first entry to 1: a^0 = 1
    v = zeros(2^m, length(g)-1);
    
    for i = 1:2^m - 1
        poly = [0 poly(1:end-1)];
        if poly(end) == 1
            poly(end) = 0;
            poly = xor(poly,g);
        end
        v(i,:) = poly(1:end-1);
    end
end